package com.flyoil.petromp.mvp_m.CourseMessageModel;

import com.cnpc.fyutils.NumberToWord;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_course_message.CourseFinanceMessageEntity;
import com.flyoil.petromp.entity.entity_order.MapListEntity;
import com.flyoil.petromp.entity.entity_order.OrderSignatureEntity;
import com.flyoil.petromp.base.BaseModel;
import com.flyoil.petromp.mvp_p.Listener.OnPresenterListener;
import com.flyoil.petromp.mvp_p.Listener.OrderMessageListener;
import com.flyoil.petromp.utils.MoccaPreferences;
import com.flyoil.petromp.utils.OrderUtl.OrderStatusUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyin on 2017/10/31.
 * 付款申请单详情界面的数据处理类
 */

public class CourseFinanceMessageModel implements BaseModel {


    @Override
    public void loadNetworkData(Map<String, Object> map, final OnPresenterListener onPresenterListener) {
        httpModel.getCourseFinanceMesage(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                if (model.getCode() != ConstantNetworkCode.SuccedCode) {
                    onPresenterListener.onsucced(null);
                }
                CourseFinanceMessageEntity entity = null;
                if (model instanceof CourseFinanceMessageEntity) {
                    entity = (CourseFinanceMessageEntity) model;
                }
                if (entity != null && entity.getData() != null) {
                    OrderMessageListener listener = (OrderMessageListener) onPresenterListener;
                    //基本信息
                    List<MapListEntity> mapListEntities = new ArrayList<>();
                    mapListEntities.add(new MapListEntity("需求名称", "null"));
                    mapListEntities.add(new MapListEntity("订单编号", entity.getData().getSn()));
                    mapListEntities.add(new MapListEntity("编制单位", entity.getData().getInstitution()));
                    mapListEntities.add(new MapListEntity("资金渠道", entity.getData().getChannel()));
                    mapListEntities.add(new MapListEntity("项目批复金额(万元)", entity.getData().getReplyAmount()));
                    mapListEntities.add(new MapListEntity("承建单位", entity.getData().getReplyAmount()));
                    mapListEntities.add(new MapListEntity("已确认项目支出(万元)", entity.getData().getUsedAmount()));
                    mapListEntities.add(new MapListEntity("收款单位名称", entity.getData().getPayee()));
                    mapListEntities.add(new MapListEntity("合同总金额(元)", entity.getData().getTotalMoney()));
                    mapListEntities.add(new MapListEntity("工程形象进度", entity.getData().getEngineeringProgress()));
                    mapListEntities.add(new MapListEntity("账号", entity.getData().getAccount()));
                    mapListEntities.add(new MapListEntity("开户银行", entity.getData().getBank()));
                    mapListEntities.add(new MapListEntity("已付金额(元)", entity.getData().getPaidAmount()));
                    mapListEntities.add(new MapListEntity("现付金额(元、大写)", NumberToWord.transfer(entity.getData().getAmountStr())));
                    listener.upDataTextsView(mapListEntities);
                    //验收执行的标准和规范的备注
                    listener.upDataRemarkView(new MapListEntity("付款内容说明", entity.getData().getRemark()));
                    //签字意见并获取签字意见列表未录入意见的数据最大的id
                    if (entity.getData().getConfirmLogListVos() != null) {
                        List<OrderSignatureEntity> confirmLogs = new ArrayList<OrderSignatureEntity>();
                        int sigantureId = 0;
                        for (int i = 0; i < entity.getData().getConfirmLogListVos().size(); i++) {
                            OrderSignatureEntity sigantureEntity = entity.getData().getConfirmLogListVos().get(i);
                            if (sigantureEntity.getAppliedTimeLabel() != null && sigantureEntity.getAppliedTimeLabel().length() > 0) {
                                confirmLogs.add(sigantureEntity);
                            } else {
                                if (sigantureEntity.getId() < sigantureId || sigantureId == 0) {
                                    sigantureId = sigantureEntity.getId();
                                }
                            }
                        }
                        //更新签字意见列表
                        listener.upDataSiganturesView(confirmLogs);
                        //更新单据的签字意见的id
                        listener.upDataOrderConfirmId(sigantureId);
                    }



                    //审核状态为待审核，单据状态流转中，显示签字意见输入框
                    if (entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_FLOW) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNAPPROVE)) {
                        listener.upDataShowSigantureView(true);
                    } else {
                        listener.upDataShowSigantureView(false);
                    }
                    //审核状态为已驳回待撤销、单据流转状态是待撤销、当前登录用户是申请人，才能显示撤销按钮
                    if (entity.getData().getUserId() == MoccaPreferences.userId.get()&&entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_UNBACKOUT) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNBACKOUT)) {
                        listener.upDataShowBackoutView(true);
                    } else {
                        listener.upDataShowBackoutView(false);
                    }
                    //审核状态为已撤销时，显示已撤销图标
                    if (entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_BACKOUTED)) {
                        listener.upDataShowRemoveImagView(true);
                    } else {
                        listener.upDataShowRemoveImagView(false);
                    }
                } else {
                    onPresenterListener.onsucced(null);
                }

            }

            @Override
            public void onFailure(String msg) {
                onPresenterListener.onsucced(null);
            }
        });
    }


}
